//index.js
//获取应用实例
const userService = require('../../services/userService');
const phoneService = require('../../services/phoneService');
var app = getApp()
Page({
  data: {
    VerifyCode:'获取验证码',
    money: '',
    phone: '',
    code: '',
    error: false,
    errorMsg: ''
  },
  validatemobile: function (event) {
    var mobile = event.detail.value;
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    return true;
  },
  bindKeyInput: function(e) {
    this.setData({
      phone: e.detail.value
    })
    //this._validatePhone(e.detail.value);
  },

  _validatePhone: function(val){
    if(!val) {
      this.setData({
        error: true,
        errorMsg: '请输入手机号！'
      });
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(val)) {
      this.setData({
        error: true,
        errorMsg: '手机号有误！'
      });
      return false;
    }
    
    this.setData({
      error: false
    });
    return true;
  },

  _validateCode: function(val){
    if(val.length == 0) {
      this.setData({
        error: true,
        errorMsg: '请输入验证码！'
      });
      return false;
    }

    this.setData({
      error: false
    });
    return true;
  },

  withDraw() {
    var money = this.data.money;
    var mobile = this.data.phone;
    var code = this.data.code;
    if(!this._validatePhone(mobile) || !this._validateCode(code)){
      return;
    }

    phoneService.verifyCode(mobile,code).then((data) => {
        if(data.code == 0) {
          this.setData({
            error: true,
            errorMsg: '验证码错误！'
          });
          return;
        } else {
          var user = wx.getStorageSync('user');
          if(user) {
            userService.withdraw(money,user.openid).then((data) => {
            if(data.code == 0){
              this.setData({
                error: true,
                errorMsg: data.msg
              });
            } else {
              wx.showModal({
                title: '提现',
                content: '提现成功',
                showCancel: false,
                confirmText: '返回首页',
                success: function(){
                  wx.navigateTo({
                    url: '/pages/index/index'
                  });
                }
              });
            }
          }, (error) => {
         });
          }
          
          
        }
      }, (error) => {
      });
  },
  bindCodeInput: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  setVerify: function (e) {//发送验证码
    var linkTel = this.data.phone;
    var mobile = this.data.phone;
    if(!this._validatePhone(mobile)){
      return;
    }
    // var _Url = Api.getVerifyCode;
    var total_micro_second = 60 * 1000;
    phoneService.getVerifyCode(linkTel).then((data)=>{
    },(error=>{
      wx.showModal({
            title: '提示',
            content: '发送验证码失败！',
            showCancel: false
          })
    }));
    //验证码倒计时
    count_down(this, total_micro_second);
  },
  onLoad: function (options) {
    if(options.money) {
      this.setData({
        money:options.money
      });
    }
    var that = this;
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  }
})

function count_down(that, total_micro_second) {
  if (total_micro_second <= 0) {
    that.setData({
      VerifyCode: "重新发送"
    });
    // timeout则跳出递归
    return;
  }
  // 渲染倒计时时钟
  that.setData({
    VerifyCode: date_format(total_micro_second) + " 秒"
  });
  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 10;
    count_down(that, total_micro_second);
  }, 10)
}
// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  // 秒位
  var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;
  // 毫秒位，保留2位
  var micro_sec = fill_zero_prefix(Math.floor((micro_second % 1000) / 10));
  return sec;
}
// 位数不足补零
function fill_zero_prefix(num) {
  return num < 10 ? "0" + num : num
}
